package com.centling.smartSealForPhone.adapter;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.activity.BluetoothListActivity;
import com.centling.smartSealForPhone.activity.RegisterActivity;

import java.util.List;

/**
 * @author fionera
 */
public class RvBlueListAdapter extends RecyclerView.Adapter<RvBlueListAdapter.MyViewHolder> {
    private Context context;
    private List<BluetoothDevice> devices;

    public RvBlueListAdapter(Context context, List<BluetoothDevice> devices) {
        this.context = context;
        this.devices = devices;
    }

    @Override
    public RvBlueListAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_bluetooth_list, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RvBlueListAdapter.MyViewHolder holder, final int position) {
        int connPosition = -1;
        int succPosition = -1;
        if(context instanceof BluetoothListActivity){
            connPosition = ((BluetoothListActivity) context).connPosition;
            succPosition = ((BluetoothListActivity) context).successPosition;
        }else if(context instanceof RegisterActivity){
            connPosition = ((RegisterActivity) context).connPosition;
            succPosition = ((RegisterActivity) context).successPosition;
        }
        if (position == connPosition) {
            holder.layoutConning.setVisibility(View.VISIBLE);
            holder.tvToConn.setVisibility(View.GONE);
        } else {
            holder.layoutConning.setVisibility(View.GONE);
            holder.tvToConn.setVisibility(View.VISIBLE);
            if (position == succPosition) {
                holder.tvToConn.setText("已连接");
            } else {
                holder.tvToConn.setText("点击连接");
            }
        }

        if (devices.get(position).getName() != null && devices.get(position).getName()
                .length() > 0) {
            holder.tvBlueName.setText("智能印章");
        } else {
            holder.tvBlueName.setText("未命名设备");
        }
        holder.tvAddress.setText(devices.get(position).getAddress());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(holder.itemView, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return devices.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tvBlueName;
        TextView tvToConn;
        LinearLayout layoutConning;
        TextView tvAddress;

        MyViewHolder(View itemView) {
            super(itemView);
            tvBlueName = itemView.findViewById(R.id.tv_device_name);
            tvToConn = itemView.findViewById(R.id.item_btlist_toconn);
            layoutConning = itemView.findViewById(R.id.item_btlist_conning);
            tvAddress = itemView.findViewById(R.id.tv_device_address);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
